<%@ page language="java" pageEncoding="UTF-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.cese.duss.ceseutil.OcrUitl" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%
    try {
        out.println( "*********************************************<br>" );
        out.println( "*----------------发票验真开始-----------------*<br>" );
        out.println( "*********************************************<br>" );
        String sql = "select fplx,fpdm,fphm,fprq,bhsje,checkCode,sellerRegisterNum from uf_fpxx t where " +
                "fplx in ('1','2','3') and xfdwsh is null and sellerRegisterNum is not null and fprq like'%2021-01%'";
        rs.executeQuery( sql );
        int sumCount=0;
        int succCount=0;
        int failCount=0;
        while (rs.next()) {
            sumCount++;
            String newinvoiceType = Util.null2String( rs.getString( "fplx" ) );
            String invoiceType = getYZTicketType( newinvoiceType );
            String invoiceCode = Util.null2String( rs.getString( "fpdm" ) );
            String invoiceNum = Util.null2String( rs.getString( "fphm" ) );
            String invoiceDate = Util.null2String( rs.getString( "fprq" ) );
            String totalAmount = Util.null2String( rs.getString( "bhsje" ) );
            String checkCode = Util.null2String( rs.getString( "checkCode" ) );
            String subCheckCode = checkCode.length() >= 6 ? checkCode.substring( checkCode.length() - 6 ) : checkCode;
            String purchaserRegisterNum = Util.null2String( rs.getString( "sellerRegisterNum" ) );//预留电话
            JSONObject returnJson = invoicerecertify( invoiceType, invoiceCode, invoiceNum, invoiceDate, totalAmount, subCheckCode, purchaserRegisterNum );
            JSONObject ReturnInfo = JSONObject.parseObject( returnJson.getString( "ReturnInfo" ) );
            String Code = ReturnInfo.getString( "Code" );
            String Message = ReturnInfo.getString( "Message" );
            if ("0".equals( Code )) {//验真通过
                JSONObject Data = JSONObject.parseObject( returnJson.getString( "Data" ) );
                String concatCode = invoiceCode+invoiceNum;
                String BuyerName = Util.null2String(Data.getString("BuyerName"));//购买方名称
                String SellerTaxpayerNo =Util.null2String( Data.getString("SellerTaxpayerNo"));//销售方税号
                String sql1 = "update uf_fpxx set gmfmc=?,xfdwsh=? where concat(fpdm,fphm)=?";
                boolean flag = rs.executeUpdate(sql1,BuyerName,SellerTaxpayerNo,concatCode );
                out.println( "======================数据更新结果返回：" + flag + "<br>");
                succCount++;
            }else {
                failCount++;
            }
            out.println( "======================发票验真接口结果返回：" + Message + "<br>");
        }
        out.println( "======================最终结果: 总计数量：["+sumCount+"];成功数量：["+succCount+"];失败数量：["+failCount+"]" );
    } catch (Exception e) {
        e.printStackTrace();
    }
%>
<%!
    //发票验真接口
    public static JSONObject getInvoiceValidate(String urlString, String API_KEY, JSONObject params) {
        Map<String, String> headers = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        headers.put( "MediaType", "application/octet-stream" );
        headers.put( "Content-Type", "application/json" );
        headers.put( "X-API-Key", API_KEY );
        jsonObject.put( "Data", params );
        String result = OcrUitl.httpPostRaw( urlString, jsonObject.toJSONString(), headers, "UTF-8" );
        JSONObject resultJson = JSONObject.parseObject( result );//发票识别的具体信息
        return resultJson;
    }

    //发票认证
    public static JSONObject invoicerecertify(String invoiceType, String invoiceCode, String invoiceNum, String invoiceDate, String totalAmount, String checkCode, String purchaserRegisterNum) {
        String YZ_API_KEY = "2d71665b-9f03-4eae-b104-3a985709a8f0";
        String yz_url = "https://api.inspuronline.com/business/cn-vat-receipt/v2/invoice-check-info";
        String msg = "";
        boolean flag;
        Map<String, Object> dataMap = new HashMap<>();
        JSONObject params = new JSONObject();
        params.put( "InvoiceType", invoiceType );//发票类型
        params.put( "InvoiceCode", invoiceCode );//发票代码
        params.put( "InvoiceNumber", invoiceNum );//发票号码
        params.put( "DateOfInvoice", invoiceDate );//开票日期
        params.put( "Amount", totalAmount );//未税金额
        params.put( "CheckCode", checkCode );//校验码后六位
        params.put( "TaxpayerIDNo", purchaserRegisterNum );//税号
        JSONObject returnJson = getInvoiceValidate( yz_url, YZ_API_KEY, params );
        return returnJson;
    }

    //获取验真发票类型
    public static String getYZTicketType(String invoiceType) {
        String newinvoiceType = "";
        switch (invoiceType) {
            case "0":
                newinvoiceType = "04";
                break;//增值税普通发票
            case "1":
                newinvoiceType = "01";
                break;//增值税专用发票
            case "14":
                newinvoiceType = "10";
                break;//增值税电子普通发票
            case "11":
                newinvoiceType = "03";
                break;//机动车销售统一发票
            case "15":
                newinvoiceType = "11";
                break;//增值税普通发票（卷式）
        }
        return newinvoiceType;
    }
%>